function [ber_antenna_div, fig_handle] = antenna_diversity(EbN0_dB, num_bits, num_antennas)
% 天线分集性能分析
% 输入:
%   EbN0_dB - 比特信噪比范围 (dB)
%   num_bits - 仿真比特数
%   num_antennas - 天线数量数组
% 输出:
%   ber_antenna_div - 天线分集误比特率矩阵
%   fig_handle - 图形句柄

    EbN0_linear = 10.^(EbN0_dB/10);
    ber_antenna_div = zeros(length(EbN0_dB), length(num_antennas));
    
    % 添加路径
    addpath('../Common');
    colors = color_definitions();
    
    fprintf('=== 天线分集性能分析 ===\n');
    
    % 不同合并技术
    combining_methods = {'选择合并', '等增益合并', '最大比合并'};
    
    for ant_idx = 1:length(num_antennas)
        M = num_antennas(ant_idx);
        fprintf('仿真 %d 天线分集...\n', M);
        
        ber_methods = zeros(length(EbN0_dB), length(combining_methods));
        
        for eb_idx = 1:length(EbN0_dB)
            fprintf('  Eb/N0 = %d dB...\n', EbN0_dB(eb_idx));
            
            % 生成随机比特
            tx_bits = randi([0, 1], num_bits, 1);
            
            % BPSK调制
            tx_symbols = 2 * tx_bits - 1;
            
            % 生成M个接收天线的信道
            channels = sqrt(0.5) * (randn(num_bits, M) + 1i * randn(num_bits, M));
            
            % 添加AWGN噪声
            noise_power = 1 / (2 * EbN0_linear(eb_idx));
            noise = sqrt(noise_power) * (randn(num_bits, M) + 1i * randn(num_bits, M));
            
            % 接收信号
            rx_symbols = tx_symbols .* channels + noise;
            
            % 1. 选择合并 (SC)
            [~, best_antenna] = max(abs(channels), [], 2);
            rx_sc = zeros(num_bits, 1);
            for bit_idx = 1:num_bits
                rx_sc(bit_idx) = rx_symbols(bit_idx, best_antenna(bit_idx));
            end
            rx_bits_sc = (real(rx_sc) > 0);
            ber_methods(eb_idx, 1) = sum(tx_bits ~= rx_bits_sc) / num_bits;
            
            % 2. 等增益合并 (EGC)
            rx_egc = sum(rx_symbols, 2);
            rx_bits_egc = (real(rx_egc) > 0);
            ber_methods(eb_idx, 2) = sum(tx_bits ~= rx_bits_egc) / num_bits;
            
            % 3. 最大比合并 (MRC)
            weights = conj(channels) ./ (abs(channels).^2 + noise_power);
            rx_mrc = sum(weights .* rx_symbols, 2);
            rx_bits_mrc = (real(rx_mrc) > 0);
            ber_methods(eb_idx, 3) = sum(tx_bits ~= rx_bits_mrc) / num_bits;
        end
        
        ber_antenna_div(:, ant_idx) = ber_methods(:, 3); % 保存MRC结果
    end
    
    % 绘制结果
    fig_handle = figure('Name', '天线分集性能', 'Position', [300, 300, 1200, 800]);
    
    % 不同合并技术比较 (固定天线数)
    subplot(2,3,1);
    M = 4; % 固定4天线
    for method_idx = 1:length(combining_methods)
        % 重新计算4天线的所有方法
        ber_4ant = zeros(length(EbN0_dB), 1);
        
        for eb_idx = 1:length(EbN0_dB)
            tx_bits = randi([0, 1], num_bits, 1);
            tx_symbols = 2 * tx_bits - 1;
            channels = sqrt(0.5) * (randn(num_bits, M) + 1i * randn(num_bits, M));
            noise_power = 1 / (2 * EbN0_linear(eb_idx));
            noise = sqrt(noise_power) * (randn(num_bits, M) + 1i * randn(num_bits, M));
            rx_symbols = tx_symbols .* channels + noise;
            
            if method_idx == 1 % SC
                [~, best_antenna] = max(abs(channels), [], 2);
                rx_sc = zeros(num_bits, 1);
                for bit_idx = 1:num_bits
                    rx_sc(bit_idx) = rx_symbols(bit_idx, best_antenna(bit_idx));
                end
                rx_bits = (real(rx_sc) > 0);
            elseif method_idx == 2 % EGC
                rx_egc = sum(rx_symbols, 2);
                rx_bits = (real(rx_egc) > 0);
            else % MRC
                weights = conj(channels) ./ (abs(channels).^2 + noise_power);
                rx_mrc = sum(weights .* rx_symbols, 2);
                rx_bits = (real(rx_mrc) > 0);
            end
            
            ber_4ant(eb_idx) = sum(tx_bits ~= rx_bits) / num_bits;
        end
        
        semilogy(EbN0_dB, ber_4ant, 'Color', colors(method_idx,:), 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('Eb/N0 (dB)');
    ylabel('误比特率 (BER)');
    title('合并技术比较 (4天线)');
    legend(combining_methods);
    
    % 不同天线数比较 (MRC)
    subplot(2,3,2);
    for ant_idx = 1:length(num_antennas)
        semilogy(EbN0_dB, ber_antenna_div(:, ant_idx), ...
                 'Color', colors(ant_idx,:), 'LineWidth', 2);
        hold on;
        
        % 理论曲线
        M = num_antennas(ant_idx);
        theory_ber = nchoosek(2*M-1, M) * (0.5 * (1 - sqrt(EbN0_linear ./ (1 + EbN0_linear)))).^M;
        semilogy(EbN0_dB, theory_ber, 'Color', colors(ant_idx,:), 'LineStyle', '--', 'LineWidth', 1);
    end
    grid on;
    xlabel('Eb/N0 (dB)');
    ylabel('误比特率 (BER)');
    title('天线分集性能 (MRC)');
    legend([arrayfun(@(x) sprintf('M=%d', x), num_antennas, 'UniformOutput', false), ...
           arrayfun(@(x) sprintf('M=%d(理论)', x), num_antennas, 'UniformOutput', false)]);
    
    % 分集增益
    subplot(2,3,3);
    ber_ref = ber_antenna_div(:, 1); % 单天线作为参考
    for ant_idx = 2:length(num_antennas)
        div_gain = ber_ref ./ ber_antenna_div(:, ant_idx);
        semilogy(EbN0_dB, div_gain, 'Color', colors(ant_idx,:), 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('Eb/N0 (dB)');
    ylabel('分集增益');
    title('天线分集增益');
    legend(arrayfun(@(x) sprintf('M=%d vs M=1', x), num_antennas(2:end), 'UniformOutput', false));
    
    % 天线间距影响
    subplot(2,3,4);
    spacing_values = [0.1, 0.5, 1.0, 2.0]; % 以波长为单位
    M = 4;
    ber_spacing = zeros(length(EbN0_dB), length(spacing_values));
    
    for sp_idx = 1:length(spacing_values)
        spacing = spacing_values(sp_idx);
        fprintf('仿真天线间距 = %.1fλ...\n', spacing);
        
        % 生成相关信道 (简化模型)
        correlation = exp(-2 * pi * spacing); % 相关性模型
        
        for eb_idx = 1:length(EbN0_dB)
            tx_bits = randi([0, 1], num_bits, 1);
            tx_symbols = 2 * tx_bits - 1;
            
            % 生成相关信道
            base_channel = sqrt(0.5) * (randn(num_bits, 1) + 1i * randn(num_bits, 1));
            channels = zeros(num_bits, M);
            for m = 1:M
                channels(:, m) = correlation^(m-1) * base_channel + ...
                                  sqrt(1 - correlation^(2*(m-1))) * sqrt(0.5) * (randn(num_bits, 1) + 1i * randn(num_bits, 1));
            end
            
            noise_power = 1 / (2 * EbN0_linear(eb_idx));
            noise = sqrt(noise_power) * (randn(num_bits, M) + 1i * randn(num_bits, M));
            rx_symbols = tx_symbols .* channels + noise;
            
            % MRC合并
            weights = conj(channels) ./ (abs(channels).^2 + noise_power);
            rx_mrc = sum(weights .* rx_symbols, 2);
            rx_bits = (real(rx_mrc) > 0);
            
            ber_spacing(eb_idx, sp_idx) = sum(tx_bits ~= rx_bits) / num_bits;
        end
    end
    
    for sp_idx = 1:length(spacing_values)
        semilogy(EbN0_dB, ber_spacing(:, sp_idx), ...
                 'Color', colors(sp_idx,:), 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('Eb/N0 (dB)');
    ylabel('误比特率 (BER)');
    title('天线间距影响 (M=4)');
    legend(arrayfun(@(x) sprintf('d=%.1fλ', x), spacing_values, 'UniformOutput', false));
    
    % 角度扩展影响
    subplot(2,3,5);
    angle_spreads = [5, 15, 30, 60]; % 度
    ber_angle = zeros(length(EbN0_dB), length(angle_spreads));
    
    for ang_idx = 1:length(angle_spreads)
        angle_spread = angle_spreads(ang_idx);
        fprintf('仿真角度扩展 = %d°...\n', angle_spread);
        
        % 角度扩展影响相关性 (简化模型)
        correlation = max(0, 1 - angle_spread/90); % 相关性随角度扩展减小
        
        for eb_idx = 1:length(EbN0_dB)
            tx_bits = randi([0, 1], num_bits, 1);
            tx_symbols = 2 * tx_bits - 1;
            
            % 生成信道
            channels = sqrt(0.5) * (randn(num_bits, M) + 1i * randn(num_bits, M));
            
            % 应用角度扩展影响
            if correlation < 1
                for m = 2:M
                    channels(:, m) = correlation * channels(:, m) + ...
                                      sqrt(1 - correlation^2) * sqrt(0.5) * (randn(num_bits, 1) + 1i * randn(num_bits, 1));
                end
            end
            
            noise_power = 1 / (2 * EbN0_linear(eb_idx));
            noise = sqrt(noise_power) * (randn(num_bits, M) + 1i * randn(num_bits, M));
            rx_symbols = tx_symbols .* channels + noise;
            
            % MRC合并
            weights = conj(channels) ./ (abs(channels).^2 + noise_power);
            rx_mrc = sum(weights .* rx_symbols, 2);
            rx_bits = (real(rx_mrc) > 0);
            
            ber_angle(eb_idx, ang_idx) = sum(tx_bits ~= rx_bits) / num_bits;
        end
    end
    
    for ang_idx = 1:length(angle_spreads)
        semilogy(EbN0_dB, ber_angle(:, ang_idx), ...
                 'Color', colors(ang_idx,:), 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('Eb/N0 (dB)');
    ylabel('误比特率 (BER)');
    title('角度扩展影响 (M=4)');
    legend(arrayfun(@(x) sprintf('Δ=%d°', x), angle_spreads, 'UniformOutput', false));
    
    % 阵列配置比较
    subplot(2,3,6);
    array_configs = {'ULA', 'UPA', 'UCA'};
    M = 4;
    ber_array = zeros(length(EbN0_dB), length(array_configs));
    
    for config_idx = 1:length(array_configs)
        config = array_configs{config_idx};
        fprintf('仿真阵列配置: %s...\n', config);
        
        for eb_idx = 1:length(EbN0_dB)
            tx_bits = randi([0, 1], num_bits, 1);
            tx_symbols = 2 * tx_bits - 1;
            
            % 不同阵列配置的信道特性
            if strcmp(config, 'ULA') % 均匀线阵
                channels = sqrt(0.5) * (randn(num_bits, M) + 1i * randn(num_bits, M));
                % 添加空间相关性
                for m = 2:M
                    channels(:, m) = 0.7 * channels(:, m-1) + ...
                                      sqrt(1-0.7^2) * sqrt(0.5) * (randn(num_bits, 1) + 1i * randn(num_bits, 1));
                end
            elseif strcmp(config, 'UPA') % 均匀面阵
                channels = sqrt(0.5) * (randn(num_bits, M) + 1i * randn(num_bits, M));
                % 更强的相关性
                for m = 2:M
                    channels(:, m) = 0.8 * channels(:, m-1) + ...
                                      sqrt(1-0.8^2) * sqrt(0.5) * (randn(num_bits, 1) + 1i * randn(num_bits, 1));
                end
            else % UCA 均匀圆阵
                channels = sqrt(0.5) * (randn(num_bits, M) + 1i * randn(num_bits, M));
                % 环形相关性
                center_channel = mean(channels, 2);
                for m = 1:M
                    channels(:, m) = 0.6 * center_channel + ...
                                      sqrt(1-0.6^2) * sqrt(0.5) * (randn(num_bits, 1) + 1i * randn(num_bits, 1));
                end
            end
            
            noise_power = 1 / (2 * EbN0_linear(eb_idx));
            noise = sqrt(noise_power) * (randn(num_bits, M) + 1i * randn(num_bits, M));
            rx_symbols = tx_symbols .* channels + noise;
            
            % MRC合并
            weights = conj(channels) ./ (abs(channels).^2 + noise_power);
            rx_mrc = sum(weights .* rx_symbols, 2);
            rx_bits = (real(rx_mrc) > 0);
            
            ber_array(eb_idx, config_idx) = sum(tx_bits ~= rx_bits) / num_bits;
        end
    end
    
    for config_idx = 1:length(array_configs)
        semilogy(EbN0_dB, ber_array(:, config_idx), ...
                 'Color', colors(config_idx,:), 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('Eb/N0 (dB)');
    ylabel('误比特率 (BER)');
    title('阵列配置比较 (M=4)');
    legend(array_configs);
    
    % 保存图片
    saveas(fig_handle, 'results/antenna_diversity_analysis.png');
end